/**
 * 
 */
package com.hahaduo.bee.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.hahaduo.bee.dao.AccountTypeDao;
import com.hahaduo.bee.dao.jpa.entity.AccountType;
import com.hahaduo.bee.dao.jpa.factory.EMF;
import com.hahaduo.bee.dto.AccountTypeDto;
import com.hahaduo.bee.util.StringUtils;

/**
 * @author noodles.wumu@gmail.com
 * @since 2013/06/13 15:58:16
 * @version 0.1
 */
@Repository
public class AccountTypeDaoImpl implements AccountTypeDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hahaduo.bee.dao.AccountTypeDao#add(com.hahaduo.bee.dto.AccountTypeDto
	 * )
	 */
	@Override
	public AccountType add(AccountTypeDto a) {

		EntityManager em = EMF.get().createEntityManager();
		try {
			AccountType at = new AccountType();
			at.setLabel1(a.getLabel1());
			at.setLabel2(a.getLabel2());
			at.setName(a.getName());
			if (StringUtils.isNotEmpty(a.getParentId())) {
				at.setParentId(Long.parseLong(a.getParentId()));
			}
			em.persist(at);
			return at;
		} finally {
			em.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hahaduo.bee.dao.AccountTypeDao#getAll()
	 */
	@Override
	public List<AccountType> getAll() {
		EntityManager em = EMF.get().createEntityManager();
		try {
			String jpql = "SELECT a FROM AccountType a";
			TypedQuery<AccountType> q = em.createQuery(jpql, AccountType.class);
			return q.getResultList();
		} finally {
			em.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hahaduo.bee.dao.AccountTypeDao#get(com.google.appengine.api.datastore
	 * .Key)
	 */
	@Override
	public AccountType get(Long id) {
		EntityManager em = EMF.get().createEntityManager();
		try {
			return em.find(AccountType.class, id);
		} finally {
			em.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hahaduo.bee.dao.AccountTypeDao#delete(java.lang.String)
	 */
	@Override
	public void delete(Long id) {
		EntityManager em = EMF.get().createEntityManager();
		try {
			em.getTransaction().begin();
			TypedQuery<AccountType> tq = em.createQuery("SELECT a FROM AccountType a WHERE a.parentId = :parentId", AccountType.class);
			tq.setParameter("parentId", id);
			List<AccountType> atList = tq.getResultList(); 
			
			for (AccountType a : atList) {
				em.remove(a);
			}
			
			em.remove(em.find(AccountType.class, id));
			em.getTransaction().commit();
		} finally {
			em.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hahaduo.bee.dao.AccountTypeDao#update(com.hahaduo.bee.dto.AccountTypeDto
	 * )
	 */
	@Override
	public AccountType update(AccountTypeDto a) {
		EntityManager em = EMF.get().createEntityManager();
		try {
			AccountType at = em.find(AccountType.class, Long.parseLong(a.getId()));
			at.setName(a.getName());
			at.setLabel1(a.getLabel1());
			at.setLabel2(a.getLabel2());
			if (StringUtils.isNotEmpty(a.getParentId())) {
				at.setParentId(Long.parseLong(a.getParentId()));
			}
			return at;
		} finally {
			em.close();
		}
	}
}
